package com.gitee.l0km.casban;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
import com.google.common.collect.Range;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkArgument;
public class RangeFilter implements Supplier<RangeFilter>{
	private final Range<Integer> range;
	private final Predicate<Object> sizeFilter;
	public RangeFilter(Range<Integer> range) {
		this.range = checkNotNull(range,"range is null");
		this.sizeFilter = Predicates.compose(this.range, CommonFunctions.sizeOfFun());
	}
	@Override
	public RangeFilter get() {
		return this;
	}

	public Range<Integer> range() {
		return range;
	}
	@SuppressWarnings("unchecked")
	public<T> Predicate<T> sizeFilter() {
		return (Predicate<T>) sizeFilter;
	}
	public <T> T checkSize(T object) {
		checkArgument(sizeFilter.apply(object),"OUT OF RANGE %s",range);
		return object;
	}
	public static RangeFilter open(int lower,int upper) {
		return new RangeFilter(Range.open(lower, upper));
	}
	public static RangeFilter closed(int lower,int upper) {
		return new RangeFilter(Range.closed(lower, upper));
	}
	public static RangeFilter closedOpen(int lower,int upper) {
		return new RangeFilter(Range.closedOpen(lower, upper));
	}
	public static RangeFilter openClosed(int lower,int upper) {
		return new RangeFilter(Range.openClosed(lower, upper));
	}
	public static RangeFilter lessThan(int endpoint) {
		return new RangeFilter(Range.lessThan(endpoint));
	}
	public static RangeFilter atMost(int endpoint) {
		return new RangeFilter(Range.atMost(endpoint));
	}
	public static RangeFilter greaterThan(int endpoint) {
		return new RangeFilter(Range.greaterThan(endpoint));
	}
	public static RangeFilter atLeast(int endpoint) {
		return new RangeFilter(Range.atLeast(endpoint));
	}
}
